package com.itcast.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itcast.dao.OrderSettingDao;
import com.itcast.pojo.OrderSetting;
import com.itcast.service.OrderSettingService;
import org.springframework.beans.factory.annotation.Autowired;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @className: OrderSettingServiceImpl
 * @description: TODO 类描述
 * @author: Jng
 * @date: 2022/4/8
 **/
@Service(interfaceClass = OrderSettingService.class)
public class OrderSettingServiceImpl implements OrderSettingService {

    @Autowired
    OrderSettingDao orderSettingDao;

    @Override
    public void add(List<OrderSetting> data) throws ParseException {
        if (data != null && data.size() > 0) {
            for (OrderSetting orderSetting : data) {

                long count = orderSettingDao.findCountByOrderDate(orderSetting.getOrderDate());
                if (count > 0) {
                    orderSettingDao.editNumberByOrderDate(orderSetting);
                } else {
                    orderSettingDao.add(orderSetting);
                }
            }
        }
    }

    @Override
    public List<Map> getOrderSettingByMonth(String date) {
        String dateBegin = date + "-1";
        String dateEnd = date + "-31";
        Map map = new HashMap();
        map.put("dateBegin", dateBegin);
        map.put("dateEnd", dateEnd);
        List<OrderSetting> list = orderSettingDao.getOrderSettingByMonth(map);
        List<Map> data = new ArrayList();

        if (list != null && list.size() > 0) {
            for (OrderSetting orderSetting : list) {
                Map orderSettingMap = new HashMap();
                orderSettingMap.put("date", orderSetting.getOrderDate().getDate());
                orderSettingMap.put("number", orderSetting.getNumber());
                orderSettingMap.put("reservations", orderSetting.getReservations());
                data.add(orderSettingMap);
            }
        }
        return data;
    }

    @Override
    public void editNumberByDate(OrderSetting orderSetting) {
        long countByOrderDate = orderSettingDao.findCountByOrderDate(orderSetting.getOrderDate());
        if (countByOrderDate>0){
            orderSettingDao.editNumberByOrderDate(orderSetting);
        }
        else
        {
            orderSettingDao.add(orderSetting);
        }
    }
}
